package de.dlrg.wms.gui.forms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;

import de.dlrg.wms.controller.controllers.AgeClassController;
import de.dlrg.wms.controller.controllers.RunController;
import de.dlrg.wms.controller.controllers.TrackController;
import de.dlrg.wms.gui.forms.customForms.AgeClassSwimmerDisciplineCombobox;
import de.dlrg.wms.gui.forms.generic.GenericForm;
import de.dlrg.wms.model.Run;
import de.dlrg.wms.model.SwimmerTrackRun;
import de.dlrg.wms.model.Track;
import de.dlrg.wms.model.transformed.AgeClassToSwimmer;

public class RunForm extends GenericForm {
	private Run runToWorkOn;
	private TrackController trackController;
	private AgeClassController ageClassController;

	private List<AgeClassSwimmerDisciplineCombobox> comboboxes = new ArrayList<AgeClassSwimmerDisciplineCombobox>();

	private List<AgeClassToSwimmer> ageClassToSwimmers;
	private RunController runController;

	private Spinner runNoSpinner;
	
	public RunForm(Composite arg0, int arg1, RunController runController) {
		this(arg0, arg1, runController, new Run().setRunNo(runController.getNextRunNo()));
	}

	/**
	 * @wbp.parser.constructor
	 */
	public RunForm(Composite arg0, int arg1, RunController runController,
			Run runToWorkOn) {
		super(arg0, arg1);

		this.runToWorkOn = runToWorkOn;
		this.trackController = runController.getTrackController();
		this.ageClassController = runController.getAgeClassController();
		this.runController = runController;

		this.center.setLayout(new GridLayout(2, false));
		this.initData();
	}

	@Override
	protected void initData() {
		this.ageClassToSwimmers = this.ageClassController
				.readAllAgeClassToSwimmer();
		List<Track> tracks = this.trackController.readAll();
		int i = 0;
		new Label(this.center, SWT.BORDER).setText("Run No");
		runNoSpinner = new Spinner(this.center, SWT.BORDER | SWT.FULL_SELECTION);
		runNoSpinner.setSelection(this.runToWorkOn.getRunNo());
		HashMap<Integer, AgeClassSwimmerDisciplineCombobox> trackNoComboMapping = new HashMap<Integer, AgeClassSwimmerDisciplineCombobox>();
		for (Track track : tracks) {
			Label trackNo = new Label(this.center, SWT.BORDER);
			trackNo.setText("Track #" + track.getTrackNumber());
			trackNoComboMapping.put(track.getTrackNumber(),
					this.createInputComboboxes(i, track));
			i++;
		}

		for (SwimmerTrackRun swimmerTrackRun : this.runToWorkOn
				.getSwimmerTrackRun()) {
			trackNoComboMapping
					.get(swimmerTrackRun.getTrack().getTrackNumber())
					.setSwimmerTrackRun(swimmerTrackRun);
		}
	}

	private AgeClassSwimmerDisciplineCombobox createInputComboboxes(int row,
			Track track) {
		AgeClassSwimmerDisciplineCombobox ageClassSwimmerDisciplineCombobox = new AgeClassSwimmerDisciplineCombobox(
				this.center, SWT.BORDER, ageClassToSwimmers, track);
		ageClassSwimmerDisciplineCombobox.setLayoutData(new GridData(SWT.FILL,
				SWT.CENTER, true, false, 1, row));
		this.comboboxes.add(ageClassSwimmerDisciplineCombobox);
		return ageClassSwimmerDisciplineCombobox;
	}

	@Override
	protected void okClicked() {
		try {
			List<SwimmerTrackRun> swimmerTrackRuns = new ArrayList<SwimmerTrackRun>();
			for (AgeClassSwimmerDisciplineCombobox ageClassSwimmerDisciplineCombobox : comboboxes) {
				if (ageClassSwimmerDisciplineCombobox.getSwimmerTrackRun()
						.isFilledOut()) {
					swimmerTrackRuns.add(ageClassSwimmerDisciplineCombobox
							.getSwimmerTrackRun());
				}
			}
			runToWorkOn.setSwimmerTrackRun(swimmerTrackRuns);
			runController.validate(runToWorkOn);
			runController.createOrUpdate(runToWorkOn);
			close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
